////
//
/// \file ./src/float_type_node.cpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#include <inc/parser/float_type_node.hpp>

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/immediate_node.hpp>
#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/ws_parser.hpp>

namespace inc {
namespace parser {

using std::string;
using boost::scoped_ptr;

/// \brief Basic constructor for a floating point type node
/// \author Chandler Carruth
/// \date 2008.10.04

FloatTypeNode::FloatTypeNode()
    : ws(new WSParser)
{}

/// \brief Empty destructor, all members are smart pointers
/// \author Chandler Carruth
/// \date 2008.10.04

FloatTypeNode::~FloatTypeNode()
{}

/// \brief Parse an floating point type in INC
/// \author Chandler Carruth
/// \date 2008.10.04
///
/// Parse the "float" keyword as a type node, followed optionally by
/// a precision parameter, such as "float< 32 >".

bool
FloatTypeNode::parse( const iterator &first,
                      const iterator &last,
                      iterator &last_parsed )
{
  assert( get_text().empty() && !precision
          && "This node was parsed more than once!" );

  // This is the required bit...
  if( std::distance( first, last ) >= 5
      && string( first, first + 5 ) == "float" )
  {
    iterator end_basic_type = first + 5;
    iterator current = end_basic_type;

    // Attempt to parse a '< ... >' parameter
    if( current != last && *current == '<' )
    {
      ++current;

      // We currently require whitespace, but it might be nice to make it
      // optional.
      if( ws->parse_ws( current, last, last_parsed ) )
      {
        current = last_parsed;

        boost::scoped_ptr<ImmediateNode> tmp_imm( new ImmediateNode );
        if( tmp_imm->parse( current, last, last_parsed ) )
        {
          current = last_parsed;
          if( ws->parse_ws( current, last, last_parsed ) )
          {
            current = last_parsed;
            if( current != last && *current == '>' )
            {
              last_parsed = current + 1;
              set_text( string( first, last_parsed ) );
              precision.swap( tmp_imm );
              return true;
            }
          }
        }
      }
    }

    last_parsed = end_basic_type;
    set_text( string( first, last_parsed ) );
    return true;
  }

  //  No valid parse found.
  last_parsed = first;
  return false;
}

/// \brief Convenience getter for the precision sub-node
/// \author Chandler Carruth
/// \date 2008.10.04

ImmediateNode* FloatTypeNode::get_precision() const
{
  return precision.get();
}

} //  end parser namespace
} //  end inc namespace
